from pydantic import BaseModel, Field
from typing import List, Optional
from datetime import datetime
from decimal import Decimal


class RecordOut(BaseModel):
    record_id: str
    device_id: str
    started_at: datetime
    ended_at: Optional[datetime]
    location: Optional[str] = None
    tags: List[str] = Field(default_factory=list)
    summary: Optional[str] = None
    class_label: Optional[str] = None
    confidence: Optional[float] = None


class ResultPoint(BaseModel):
    t: float
    metric_name: str
    value: float


class RecordDetailOut(RecordOut):
    series: List[ResultPoint] = Field(default_factory=list)


class HeatmapOut(BaseModel):
    record_id: str
    width: int
    height: int
    data: List[List[float]]


# 4G信号采集相关模型
class SignalCollectionIn(BaseModel):
    """4G信号采集数据输入模型"""
    device_id: str = Field(..., description="设备ID")
    signal_strength: int = Field(..., description="4G信号强度 (dBm)", ge=-150, le=0)
    latitude: Decimal = Field(..., description="GPS纬度", ge=-90, le=90)
    longitude: Decimal = Field(..., description="GPS经度", ge=-180, le=180)
    altitude: Optional[Decimal] = Field(None, description="GPS海拔 (米)")
    accuracy: Optional[Decimal] = Field(None, description="GPS精度 (米)")
    network_type: Optional[str] = Field(None, description="网络类型")
    operator_name: Optional[str] = Field(None, description="运营商名称")
    cell_id: Optional[str] = Field(None, description="基站ID")
    lac: Optional[str] = Field(None, description="位置区域码")
    mcc: Optional[str] = Field(None, description="移动国家码")
    mnc: Optional[str] = Field(None, description="移动网络码")
    rsrp: Optional[int] = Field(None, description="RSRP值 (dBm)")
    rsrq: Optional[Decimal] = Field(None, description="RSRQ值 (dB)")
    rssi: Optional[int] = Field(None, description="RSSI值 (dBm)")
    snr: Optional[Decimal] = Field(None, description="信噪比 (dB)")
    collected_at: datetime = Field(..., description="采集时间")


class SignalCollectionOut(BaseModel):
    """4G信号采集数据输出模型"""
    id: int
    device_id: str
    signal_strength: int
    latitude: Decimal
    longitude: Decimal
    altitude: Optional[Decimal]
    accuracy: Optional[Decimal]
    network_type: Optional[str]
    operator_name: Optional[str]
    cell_id: Optional[str]
    lac: Optional[str]
    mcc: Optional[str]
    mnc: Optional[str]
    rsrp: Optional[int]
    rsrq: Optional[Decimal]
    rssi: Optional[int]
    snr: Optional[Decimal]
    collected_at: datetime
    uploaded_at: datetime
    created_at: datetime
    updated_at: Optional[datetime]

    class Config:
        orm_mode = True


class DeviceInfoIn(BaseModel):
    """设备信息输入模型"""
    device_id: str = Field(..., description="设备ID")
    device_name: Optional[str] = Field(None, description="设备名称")
    device_model: Optional[str] = Field(None, description="设备型号")
    os_version: Optional[str] = Field(None, description="操作系统版本")
    app_version: Optional[str] = Field(None, description="APP版本")


class DeviceInfoOut(BaseModel):
    """设备信息输出模型"""
    id: int
    device_id: str
    device_name: Optional[str]
    device_model: Optional[str]
    os_version: Optional[str]
    app_version: Optional[str]
    last_seen: Optional[datetime]
    is_active: bool
    created_at: datetime
    updated_at: datetime

    class Config:
        orm_mode = True


class SignalStatisticsOut(BaseModel):
    """信号统计输出模型"""
    device_id: str
    total_collections: int
    avg_signal_strength: float
    min_signal_strength: int
    max_signal_strength: int
    first_collection: datetime
    last_collection: datetime
    collection_days: int

    class Config:
        orm_mode = True